% ALGORITHM
%
% - for each laser transition event:
%   - compute the distances with all the door transitions events
%   - select that door transition that has minimal distance
% 
% the outputs are integers like '1', '2', etc. and refer to the index
% of the door transition, i.e., the first door transition, the second, etc.
%
function [	aiLaser1TransitionsEventsLabels,							...
			aiLaser2TransitionsEventsLabels	] =							...
				AssociateLaserTransitionsToDoorTransitions(				...
					aaiDoorTransitions,									...
					aaiLaser1Transitions,								...
					aaiLaser2Transitions								)
	%
	% for readability
	iNumberOfDoorTransitions 	= numel(aaiDoorTransitions(:, 1));
	iNumberOfLaser1Transitions 	= numel(aaiLaser1Transitions(:, 1));
	iNumberOfLaser2Transitions 	= numel(aaiLaser2Transitions(:, 1));
	%
% 	% DEBUG
% 	fprintf('%d door transitions\n%d laser 1 transitions\n%d laser 2 transitions\n', iNumberOfDoorTransitions, iNumberOfLaser1Transitions, iNumberOfLaser2Transitions );
% 	aaiDoorTransitions
% 	aaiLaser1Transitions
% 	aaiLaser2Transitions
	%
	% initialization
	aiLaser1TransitionsEventsLabels = zeros( iNumberOfLaser1Transitions , 1 );
	aiLaser2TransitionsEventsLabels = zeros( iNumberOfLaser2Transitions , 1 );
	%
	aafDistancesBetweenDoorTransitionsAndLaser1Transitions = zeros( iNumberOfDoorTransitions, iNumberOfLaser1Transitions );
	aafDistancesBetweenDoorTransitionsAndLaser2Transitions = zeros( iNumberOfDoorTransitions, iNumberOfLaser2Transitions );
	%
	% --------------------------------------------------------------------------
	% compute all the distances
	for iDoorTransition = 1:iNumberOfDoorTransitions;
		%
		for iLaser1Transition = 1:iNumberOfLaser1Transitions;
			%
			aafDistancesBetweenDoorTransitionsAndLaser1Transitions(iDoorTransition, iLaser1Transition) = 	...
				PeopleCounters.GetDistanceBetweenTwoTransitions(								...
						aaiLaser1Transitions(iLaser1Transition, :),						...
						aaiDoorTransitions(iDoorTransition, :)							);
			%
		end;% cycle on laser 1
		%
		for iLaser2Transition = 1:iNumberOfLaser2Transitions;
			%
			aafDistancesBetweenDoorTransitionsAndLaser2Transitions(iDoorTransition, iLaser2Transition) = 	...
				PeopleCounters.GetDistanceBetweenTwoTransitions(								...
						aaiLaser2Transitions(iLaser2Transition, :),						...
						aaiDoorTransitions(iDoorTransition, :)							);
			%
		end;% cycle on laser 2
		%
	end;% cycle on door
	%
	% --------------------------------------------------------------------------
	% find the minimal ones
	[ ~, aiLaser1TransitionsEventsLabels ] = ...
		min( aafDistancesBetweenDoorTransitionsAndLaser1Transitions, [], 1 );
	%
	[ ~, aiLaser2TransitionsEventsLabels ] = ...
		min( aafDistancesBetweenDoorTransitionsAndLaser2Transitions, [], 1 );
	%
	%
	% DEBUG
% 	aaiDoorTransitions
% 	aaiLaser1Transitions
% 	aaiLaser2Transitions
% 	aafDistancesBetweenDoorTransitionsAndLaser1Transitions
% 	aafDistancesBetweenDoorTransitionsAndLaser2Transitions
% 	aiLaser1TransitionsEventsLabels
% 	aiLaser2TransitionsEventsLabels
% 	pause
	%
end % function

